/*var person = {'name': 'rainman', 'age': 24};

localStorage.setItem("me", JSON.stringify(person));

console.log(JSON.parse(localStorage.getItem('me')).name);  // 'rainman'
*/
/**

 * JSON.stringify，将JSON数据转化为字符串

 *     JSON.stringify({'name': 'fred', 'age': 24});   // '{"name":"fred","age":24}'

 *     JSON.stringify(['a', 'b', 'c']);               // '["a","b","c"]'

 * JSON.parse，反解JSON.stringify

 *     JSON.parse('["a","b","c"]')                    // ["a","b","c"]

 */
//添加
function Storage(warehouse,data){ 
	    for(var l in localStorage){
	    	//读取 key
	     	if(l==warehouse){
	     		//读取本地 data key
	     	 	for(var i in JSON.parse(localStorage.getItem(warehouse))){
				  console.log(i+"---------");
				  //读取传入的key
				  	for(var z in data){
				 		console.log(z+"--"+data[z]);
				 		//x相等 重新赋值
          				if(i==z){
          				console.log("++++++"+JSON.parse(localStorage.getItem(warehouse))[i]);
          				JSON.parse(localStorage.getItem(warehouse))[i]=data[z];
          		//如果 data 中 无 本地的 某条数据  加入
         				 }else{
               				data[i]=JSON.parse(localStorage.getItem(warehouse))[i];
          				 }
					}
				}
	     	}else{
	     		   localStorage.setItem(warehouse,JSON.stringify(data));
	     	}
	    }
	    
}
//删除一条
function removes(warehouse,flag){
	if(flag==true){
		for(var l in localStorage){
			if(warehouse==l){
				localStorage.removeItem(l);
			}
		}
	}else{
		localStorage.clear();
	}
}

Storage("you",{'names': 'rainman22d', 'agef':2244,'sexx':"nans"});
Storage("you3",{'names': 'rainman22d', 'agef':2244,'sexx':"nans"});
Storage("you",{'names': 'rainman22dww', 'agef':2244,'sexx':"nans",'sexee':"nans"});
Storage("you2",{'names': 'rainman22d', 'agef':2244,'sexx':"nans"});
removes("you",false);
// 找到显示数据存储容器
/*var storageData = document.getElementById('storage-data')*/

// 设置数据
/*function setStorage(event) {
  var setForm = document.getElementById('set-form')
  localStorage.setItem('name', setForm.elements['name'].value)
  localStorage.setItem('gender', setForm.elements['gender'].value)
  displayStorageData()
  event.preventDefault()
}

// 显示数据*/
/*function displayStorageData() {
  if (localStorage.length > 0) {
    storageData.innerHTML = ''
    for (var i = 0; i < localStorage.length; i++) {
      var key = localStorage.key(i)
      var value = localStorage.getItem(key)
      storageData.innerHTML += '<dt>' + key + '</dt>' + '<dd>' + value + '</dd>'
    }
  }
}
*/
/*function clearStorage(event) {
  localStorage.clear()
  displayStorageData()
  storageData.innerHTML = ""
  event.preventDefault()
}*/

// 处理 storage 事件
/*function storageEventHandle (event) {
  console.log('数据名称', event.key);
  console.log('以前的值', event.oldValue);
  console.log('新的值', event.newValue);
  console.log('链接', event.url);
  console.log('存储', event.storageArea);
}*/

// 监听 设置数据项目 按钮
/*ar setStorageBtn = document.getElementById('set-storage')
setStorageBtn.addEventListener('click', setStorage, false)

// 监听 删除数据项目 按钮
var clearStorageBtn = document.getElementById('clear-storage')
clearStorageBtn.addEventListener('click', clearStorage, false)

// 监听存储区数据变化事件
window.addEventListener('storage', storageEventHandle, false)
*/